//===================================================================================
// $$
//
// Coder: Code Milker v1.0
// 0 - EntityName
// 1 - lower
//===================================================================================


namespace App.Data.SqlServer.Repositories {
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using App.Model;

    public class PictureRepository : BaseRepository, IPictureRepository {
        public PictureRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork) {
        }
        public void Create(Picture picture) {
            this.GetDbSet<Picture>().Add(picture);
            this.UnitOfWork.SaveChanges();
        }

        public Picture GetPicture(Guid pictureId) {
            return this.GetDbSet<Picture>()
                .Where(c => c.Id == pictureId)
                .Single();
        }

        public void Update(Picture updatedPicture) {
            this.GetDbSet<Picture>().Attach(updatedPicture);
            this.SetEntityState(updatedPicture, updatedPicture.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid pictureId) {
            Picture PictureToDelete = this.GetPicture(pictureId);
            this.GetDbSet<Picture>().Remove(PictureToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IQueryable<Picture> Find()
        {
            return this.GetDbSet<Picture>();
        }

        public IQueryable<Picture> GetAll()
        {
            return this.GetDbSet<Picture>();
        }
    }
}
